<template>
  <div class="form-single-fragment" style="position: relative">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      size="mini"
      label-position="right"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="商机名称" prop="oppoName">
            <el-input
              class="input-item"
              v-model="ruleForm.oppoName"
              placeholder="请输入"
              maxlength="20"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商机地区" prop="oppoDistrictCode">
            <el-cascader
              v-model="ruleForm.oppoDistrictCode"
              ref="ref_areaCodes"
              :options="areaCodelist"
              :props="areaCodeprops"
              :show-all-levels="false"
              clearable
              @change="areaCodesChange"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务类型" prop="businessType">
            <el-cascader
              v-model="ruleForm.businessType"
              ref="ref_business"
              :options="businesstypeTree"
              :props="businesstProps"
              :show-all-levels="false"
              clearable
              @change="businessChange"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属阶段" prop="stage">
            <el-select v-model="ruleForm.stage" placeholder="请选择">
              <el-option label="立项阶段" :value="1"></el-option>
              <el-option label="工作方案阶段" :value="2"></el-option>
              <el-option label="谈判阶段" :value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商机来源">
            <el-input
              class="input-item"
              v-model="ruleForm.oppoSource"
              placeholder="请输入"
              maxlength="10"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="director">
            <el-select
              v-model="ruleForm.director"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in listUsers"
                :key="item.userId"
                :label="item.showName"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参与人">
            <el-select
              v-model="ruleForm.listPartner"
              multiple
              collapse-tags
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in listUsers"
                :key="item.userId"
                :label="item.showName"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计合同额度(万元)" prop="contractAmount">
            <el-input
              type="number"
              class="input-item"
              v-model="ruleForm.contractAmount"
              placeholder="请输入"
              maxlength="20"
              oninput="if(value.indexOf('.')>0){value=value.slice(0,value.indexOf('.')+3)}if(value<0){value='0'}"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计成交时间" prop="tradingDate">
            <el-date-picker
              v-model="ruleForm.tradingDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否投标" prop="isBid">
            <el-select v-model="ruleForm.isBid" placeholder="请选择">
              <el-option label="是" :value="1"></el-option>
              <el-option label="否" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12" v-if="ruleForm.isBid==1">
          <el-form-item label="投标标段" prop="bidSection">
            <el-select v-model="ruleForm.bidSection" placeholder="请选择">
              <el-option label="标段一" :value="1"></el-option>
              <el-option label="标段二" :value="2"></el-option>
              <el-option label="标段三" :value="3"></el-option>
              <el-option label="标段四" :value="4"></el-option>
              <el-option label="标段五" :value="5"></el-option>
              <el-option label="标段六" :value="6"></el-option>
              <el-option label="标段七" :value="7"></el-option>
              <el-option label="标段八" :value="8"></el-option>
              <el-option label="标段九" :value="9"></el-option>
              <el-option label="标段十" :value="10"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item style="padding: 20px 0">
            <el-button @click="onCancel(false)">取消</el-button>
            <el-button type="primary" @click="submitForm()">确定</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { BusinessOpportunities } from '@/api/commercial.js';
import { Businesstype } from '@/api/business.js';
import { EcologicalkKnowledge } from '@/api/knowledgeBases.js';
import SysUserController from '../../api/Controller/SysUserController';
export default {
  name: 'Oppoadd',
  props: {
    oppoId: {
      default: undefined
    }
  },
  data () {
    return {
      loading: false,
      isShowloading: false,
      areaCodelist: [],
      areaCodeprops: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      },
      businesstypeTree: [],
      businesstProps: {
        value: 'id',
        label: 'businessName',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      },
      listUsers: [],
      ruleForm: {
        oppoName: '',
        businessType: '',
        oppoDistrictCode: '',
        stage: '',
        isBid: '',
        oppoSource: '',
        director: '',
        listPartner: '',
        contractAmount: '',
        bidSection: undefined,
        tradingDate: ''
      },
      rules: {
        oppoName: [{ required: true, message: '请输入', trigger: 'blur' }],
        businessType: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        oppoSource: [{ required: true, message: '请选择', trigger: 'blur' }],
        oppoDistrictCode: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        stage: [{ required: true, message: '请选择', trigger: 'blur' }],
        isBid: [{ required: true, message: '请选择', trigger: 'blur' }],
        director: [{ required: true, message: '请选择', trigger: 'blur' }],
        contractAmount: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        tradingDate: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  created () {},
  computed: {
    isEdit () {
      return this.oppoId != null && this.oppoId !== '';
    }
  },
  mounted () {
    if (this.isEdit) {
      this.loadOpportunitiesdetail();
    } else {
      this.loadbusinesstypeTree();
      this.loadareaCodelistDict();
      this.listGroupUser();
    }
  },
  methods: {
    /**
     * 商机详情
     */
    loadOpportunitiesdetail () {
      let params = this.oppoId;
      return new Promise((resolve, reject) => {
        BusinessOpportunities.detail(this, params)
          .then((res) => {
            this.ruleForm = res.data;
            this.ruleForm.oppoDistrictCode = Number(
              this.ruleForm.oppoDistrictCode
            );
            this.loadbusinesstypeTree();
            this.loadareaCodelistDict();
            this.listGroupUser();
          })
          .catch((e) => {
            reject(e);
          });
      });
    },
    /**
     * 业务类型信息表树形列表
     */
    loadbusinesstypeTree () {
      return new Promise((resolve, reject) => {
        Businesstype.tree(this)
          .then((res) => {
            this.businesstypeTree = res.data;
          })
          .catch((e) => {
            reject(e);
          });
      });
    },
    businessChange () {
      this.$refs['ref_business'].dropDownVisible = false;
    },
    /**
     * 行政区划
     */
    loadareaCodelistDict () {
      return new Promise((resolve, reject) => {
        EcologicalkKnowledge.listDict(this)
          .then((res) => {
            let areaCodelist = res.data;
            let areaCodelistnew = [];
            areaCodelist.forEach((item, index) => {
              if (!item.parentId) {
                item.children = [];
                areaCodelistnew.push(item);
              }
            });
            areaCodelistnew.forEach((item1, index1) => {
              let info = areaCodelist.find((item2, index2) => {
                return item1.id === item2.parentId;
              });
              if (info) {
                item1.children = [];
                areaCodelist.forEach((item3, index3) => {
                  if (item1.id === item3.parentId) {
                    item1.children.push(item3);
                  }
                });
              } else {
                item1.children = null;
              }
            });
            areaCodelistnew.forEach((item4, index4) => {
              if (item4.children) {
                item4.children.forEach((item5, index5) => {
                  let info2 = areaCodelist.find((item6, index6) => {
                    return item5.id === item6.parentId;
                  });
                  if (info2) {
                    item5.children = [];
                    areaCodelist.forEach((item7, index7) => {
                      if (item5.id === item7.parentId) {
                        item5.children.push(item7);
                      }
                    });
                  } else {
                    item5.children = null;
                  }
                });
              }
            });
            this.areaCodelist = areaCodelistnew;
          })
          .catch((e) => {
            reject(e);
          });
      });
    },
    /**
     * 选择商机行政区划
     */
    areaCodesChange () {
      this.$refs['ref_areaCodes'].dropDownVisible = false;
    },
    /**
     * 人员列表
     */
    listGroupUser () {
      let params = {
        orderParam: [{ fieldName: 'createTime', asc: 1 }],
        sysUserDtoFilter: {}
      };
      return new Promise((resolve, reject) => {
        SysUserController.list(this, params)
          .then((res) => {
            this.listUsers = res.data.dataList;
          })
          .catch((e) => {
            reject(e);
          });
      });
    },
    /**
     * 提交表单
     */
    submitForm () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isShowloading = true;
          let params = this.ruleForm;
          if (this.oppoId) {
            // 修改商机
            return new Promise((resolve, reject) => {
              BusinessOpportunities.update(this, params)
                .then((res) => {
                  this.$message({
                    showClose: true,
                    message: '操作成功！',
                    type: 'success'
                  });
                  this.onCancel(true);
                })
                .catch((e) => {
                  reject(e);
                });
            });
          } else {
            // 添加商机
            return new Promise((resolve, reject) => {
              BusinessOpportunities.add(this, params)
                .then((res) => {
                  this.$message({
                    showClose: true,
                    message: '操作成功！',
                    type: 'success'
                  });
                  this.onCancel(true);
                })
                .catch((e) => {
                  reject(e);
                });
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel (isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess);
      }
    }
  }
};
</script>

<style scoped>
h4 {
  margin-bottom: 0;
}
.table_container {
  margin: 10px 0;
}
.el-form-info {
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
}
.el-form-info strong {
  margin: 0 10px;
}
.form-single-fragment .el-date-editor.el-input,
.form-single-fragment .el-select,
.form-single-fragment .el-cascader--mini {
  width: 100%;
}
</style>
